<?php

namespace App\Http\Controllers\V1\Admin;

use App\Models\V1\Advert;
use App\Models\V1\Managenment\MerchantBalance;
use App\Models\V1\Pay\CashRecord;
use Illuminate\Http\Request;

class AdvertController extends BaseController
{

    /**
     * 广告列表
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $query=Advert::with(['AdvertPlace']);
        $pageSize=$request->input('pageSize',10);
        $order=$request->input('order','desc');
        $sort=$request->input('sort','id');
        if($request->filled('title')){
            $query->where('title','like',"%".$request->input('title',"")."%");
        }
        if($request->filled('advert_place_id')){
            $query->where('advert_place_id',$request->input('advert_place_id',0));
        }
        if($request->filled('state')){
            $query->where('state',$request->input('state',0));
        }
        if($request->filled('begin_at')){
            $query->where('created_at','>=',$request->input('begin_at'));
        }
        if($request->filled('end_at')){
            $query->where('created_at','<=',$request->input('end_at'));
        }
        $query->orderBy($sort,$order);
        $data=$query->paginate($pageSize);
        return ajax_output(200,"成功",$data);
    }


    /**
     * 购买盒子广告
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $model=new Advert();
        $model->create([
            'advert_place_id'=>$request->input('advert_place_id',0),
            'title'=>$request->input('title',""),
            'jump_url'=>$request->input('jump_url',""),
            'thumb'=>$request->input('thumb',""),
            'begin_at'=>$request->input('begin_at'),
            'end_at'=>$request->input('end_at'),
            'state'=>$request->input('state',1)
        ]);
        return ajax_output(200,"成功");
    }

    /**
     * 盒子广告详情
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $vo=Advert::find($id);
        return ajax_output(200,"成功",$vo);
    }


    /**
     * 更新盒子广告
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $vo=Advert::find($id);
        $vo->update([
            'advert_place_id'=>$request->input('advert_place_id',0),
            'title'=>$request->input('title',""),
            'jump_url'=>$request->input('jump_url',""),
            'thumb'=>$request->input('thumb',""),
            'begin_at'=>$request->input('begin_at'),
            'end_at'=>$request->input('end_at'),
            'state'=>$request->input('state',1)
        ]);
        return ajax_output(200,"成功");
    }

    /**
     * 删除广告
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {

        Advert::destroy($id);
        return ajax_output(200,"成功");
    }
}
